'use strict';

/**
 * @name DemandListCtrl
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('DemandListCtrl', ['$scope', '$rootScope', '$state', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaFilter', 'MetaSearch', 'MetaSort', 'MetaUtils', function($scope, $rootScope, $state, $stateParams, MetaResources, MetaRemind, MetaFilter, MetaSearch, MetaSort, MetaUtils) {
    $scope.listCtrl = {
        filterConfig: ['area_choice', 'fund', 'rank', 'province', 'manager', 'state', 'created', 'verified'],
        filterParams: {},
        searchConfig: {
          sn_like: '编号',
          title_like: '需求名称'
        },
        searchParams: {},
        sortParams: '-created_at',
        tableParams: {}
    };
    $scope.init = function() {
        MetaUtils.initListCtrl($scope, $stateParams);
    };
    $scope.getListData = function(page) {
        var queryParams = {
            '_embed': 'publisher,leader,projectManager',
            '_sort': '',
            '_range': '',
            'areas': '',
            'projectManagerId': '',
            'enterprise_info.province': $stateParams.province,
            'state': $stateParams.state,
            'version': 'online'
        };
        var dynamicQueryParams = ['fund','rank'];
        MetaUtils.buildQueryParams(page, queryParams, dynamicQueryParams, $scope.listCtrl, $stateParams);
        MetaResources.demand.query(
            queryParams,
            function(data, header) {
                MetaUtils.receiveListData($scope.listCtrl, $stateParams, page, data, header);
                //如果是用户需求，就查对应解决方案的数量，如果是合作意向，就查对应想合作的技术方案的名字
                angular.forEach(data, function(value) {
                    value.num = 0;
                    value.techName = '';
                    MetaResources.demand.queryNum({
                            'demand_id': value.id,
                            '_range': '1_1',
                            '_embed': 'technology'
                        },
                        function(data, header) {
                            if ($state.includes('meta.permission.demand.user')) {
                                value.num = parseInt(header()['x-count']);
                            } else {
                                value.techName = data[0].technology.title;
                            }
                        },
                        function(error) {
                            MetaRemind.serverError(error);
                        });
                });
            },
            function(error) {
                MetaRemind.serverError(error);
            });
    };
    $scope.changeUrl = function() {
        var urlParams = {
          page: $scope.listCtrl.tableParams.currentPage,
          sort: $scope.listCtrl.sortParams,
          showAll: $scope.listCtrl.tableParams.isShowAll
        };
        MetaFilter.encodeURLFilterParams(urlParams, $scope.listCtrl.filterParams);
        MetaSearch.encodeUrlSearchParams(urlParams, $scope.listCtrl.searchParams);
        $state.go(
          $state.includes('meta.permission.demand.user') ? 'meta.permission.demand.user.list' : 'meta.permission.demand.cooperation.list',
          urlParams);
    };
    $scope.init();
}]);
